package com.tepth.vehiclemaintenance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.bean.TroubleSuggestSection;
import com.tepth.vehiclemaintenance.bean.TroubleType;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by pc on 2017/3/16.
 */

public class TroubleSuggestTypeAdapter extends BaseSectionQuickAdapter<TroubleSuggestSection, BaseViewHolder> {
    private LayoutInflater mInflater;
    private OnTagClickListener mListener;
    private Context mContext;

    public TroubleSuggestTypeAdapter(Context context, int layoutResId,
                                     int sectionHeadResId, List data,
                                     OnTagClickListener listener) {
        super(layoutResId, sectionHeadResId, data);
        mInflater = LayoutInflater.from(context);
        mListener = listener;
        mContext = context;
    }

    @Override
    protected void convertHead(BaseViewHolder helper, TroubleSuggestSection item) {
        helper.setText(R.id.tv_header, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, TroubleSuggestSection item) {

        TagFlowLayout tagFlowLayout = helper.getView(R.id.id_tag_flow_layout);
        final TagAdapter<TroubleType> tagAdapter =
                new TagAdapter<TroubleType>(item.t) {
                    @Override
                    public View getView(FlowLayout parent, int position,
                                        TroubleType troubleType) {
                        TextView tv = (TextView) mInflater.inflate(R.layout.view_tv,
                                parent, false);
//                tv.setBackgroundDrawable(mContext.getResources()
//                        .getDrawable(R.drawable.bg_tag_single_choose));
                        tv.setText(troubleType.getDriverRemark());
                        if (troubleType.isHasSelected()) {
                            tv.setTextColor(mContext.getResources().getColor(R.color.green_scan_to_barcode_p));
                        } else {
                            tv.setTextColor(mContext.getResources().getColor(R.color.color_black));
                        }
                        return tv;
                    }
                };
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                mListener.onTagClick(tagAdapter.getItem(position));
                return false;
            }
        });
        tagFlowLayout.setMaxSelectCount(1);
        tagFlowLayout.setAdapter(tagAdapter);
    }

    public interface OnTagClickListener {
        void onTagClick(TroubleType troubleType);
    }
}
